﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AddressBook.DAO;
using AddressBook.Entity;
using System.Text;

public partial class GetContacts : System.Web.UI.Page
{
    static Base_EmployeeDAO dao = new Base_EmployeeDAO();

    protected void Page_Load(object sender, EventArgs e)
    {
        ResultEntity result = new ResultEntity();
        result.Name = "Contacts";

        if (!string.IsNullOrEmpty(Request.QueryString["deptid"]))
        {
            DateTime updatetime = DateTime.Parse("1900-01-01");
            if (!string.IsNullOrEmpty(Request.QueryString["updatetime"]))
            {
                DateTime.TryParse(Request.QueryString["updatetime"], out updatetime);
            }
            StringBuilder sb = new StringBuilder();
            List<Base_Employee> contacts = dao.GetInfos(" * ", " where DeptID = '" + Request.QueryString["deptid"] + "' and UpdateTime >= '" + updatetime.ToString("yyyy-MM-dd HH:mm:ss") + "'", string.Empty, string.Empty);
            foreach (Base_Employee employee in contacts)
            {
                if (employee.isDelete)
                {
                    sb.Append("{");
                    sb.AppendFormat("\"EmployID\":\"{0}\",", employee.EmployID);
                    sb.AppendFormat("\"DeptID\":\"{0}\",", employee.DeptID);
                    if (employee.isDelete)
                    {
                        //如果用户被删除，则返回删除标识，客户端需要删除该用户
                        sb.AppendFormat("\"isDelete\":\"1\"");
                    }
                    sb.Append("},");
                }
                else
                {
                    sb.Append("{");
                    sb.AppendFormat("\"EmployID\":\"{0}\",", employee.EmployID);
                    sb.AppendFormat("\"DeptID\":\"{0}\",", employee.DeptID);
                    sb.AppendFormat("\"FstName\":\"{0}\",", employee.FstName);
                    sb.AppendFormat("\"SecName\":\"{0}\",", employee.SecName);
                    sb.AppendFormat("\"Phone\":\"{0}\",", employee.Phone);
                    sb.AppendFormat("\"Station\":\"{0}\",", employee.Station);
                    sb.AppendFormat("\"HeadUrl\":\"{0}\"", employee.HeadUrl);
                    sb.Append("},");
                }
            }
            if (sb.Length > 0)
            {
                sb.Remove(sb.Length - 1, 1);
            }

            result.ResultCode = "0";
            result.Contents = sb.ToString();
        }
        else
        {
            result.Desc = "参数不正确！";
            result.ResultCode = "-2";
        }

        Response.Write(Common.ReturnResult(result));
    }
}